
<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class whyshopseckill_mdl_whyshopseckill extends dbeav_model 
{
  public $defaultOrder = 'whyshopseckill_id DESC';

  public function save(&$data,$mustUpdate = null,$mustInsert = false){
    if(!$data['whyshopseckill_id'])
    {
      $data['add_time']=time();
      $data['last_edit_time']=$data['add_time'];
    }
    else{
     $data['last_edit_time']=time();

   }
   return parent::save($data,$mustUpdate);
 }

 public function insert(&$data){

  return parent::insert($data);
}

public function getList($cols='*', $filter='null', $offset=0, $limit=-1, $orderby=null)
{
  $list = parent::getList($cols, $filter, $offset, $limit, $orderby);

  return $list;
}

    //获取秒杀时间段

public function  getSeckillTime(){

  $seckilltime=array();

  $showtime=date("H");


  /*昨天*/

  //$res=array("9",'15','21');
$today_time=strtotime(date("Y-m-d"));
    $tomorrow_time=strtotime(date("Y-m-d")." 23:59:59");
    //只查询今天的
    $sql="select * from sdb_whyshopseckill_whyshopseckill where seckill_starttime>=".$today_time." and seckill_endtime<=".$tomorrow_time." order by seckill_starttime asc";

    $res=$this->db->select($sql);
    $now_time=time();//
    $alllsub=array();
  foreach ($res as $key => $row) {
        $res[$key]['short_time']=date('H',$row['seckill_starttime']).":00";

         if($row['seckill_starttime']>$now_time){
        }
         elseif($row['seckill_starttime']<=$now_time && $row['seckill_endtime']>$now_time){
             $res[$key]['subtime']=  $showtime-date('H',$row['seckill_starttime']);//当前时间减去 开始小时
             $alllsub[$row['whyshopseckill_id']]=$res[$key]['subtime'];

         }
        elseif($row['seckill_endtime']<$now_time){
            $res[$key]['subtime']=  $showtime-date('H',$row['seckill_starttime']);//当前时间减去 开始小时
            $alllsub[$row['whyshopseckill_id']]=$res[$key]['subtime'];


        }


    }
    asort($alllsub);

    $inactvie_id='';
    foreach ($alllsub as $key => $row) {
        $inactvie_id=$key;
        break;
    }

        foreach($res as $key=>$row){

                if ($row['whyshopseckill_id'] == $inactvie_id) {
                    $res[$key]['isactive'] = 'skactive';
                    $res[$key]['static_org']=0;
                    $res[$key]['seckill_static']='疯抢中';
                }
                else{
                    if($row['seckill_starttime']>$now_time){
                        $res[$key]['static_org']=1;
                        $res[$key]['seckill_static']='未开始';
                    }
                    elseif($row['seckill_starttime']<=$now_time && $row['seckill_endtime']>$now_time){
                        $res[$key]['static_org']=0;
                        $res[$key]['seckill_static']='已开抢';

                    }
                    else{
                        //$res[$key]['static_org']=2;
                       // $res[$key]['seckill_static']='已结束';

                    }
                }

        }
    //print_r($res);exit;
  return $res;

}

//首页获取秒杀时间段
function getSeckillTime_index(){
       $seckilltime=array();

  $showtime=date("H");


  /*昨天*/

  //$res=array("9",'15','21');
$today_time=strtotime(date("Y-m-d"));
    $tomorrow_time=strtotime(date("Y-m-d")." 23:59:59");
    //只查询今天的
    $sql="select * from sdb_whyshopseckill_whyshopseckill where seckill_starttime>=".$today_time." and seckill_endtime<=".$tomorrow_time." order by seckill_starttime asc";

    $res=$this->db->select($sql);

    $now_time=time();//
    $alllsub=array();
  foreach ($res as $key => $row) {
        $res[$key]['short_time']=date('H',$row['seckill_starttime']).":00";

         if($row['seckill_starttime']>$now_time){
        }
         elseif($row['seckill_starttime']<=$now_time && $row['seckill_endtime']>$now_time){
             $res[$key]['subtime']=  $showtime-date('H',$row['seckill_starttime']);//当前时间减去 开始小时
             $alllsub[$row['whyshopseckill_id']]=$res[$key]['subtime'];

         }
        elseif($row['seckill_endtime']<$now_time){
            $res[$key]['subtime']=  $showtime-date('H',$row['seckill_starttime']);//当前时间减去 开始小时
            $alllsub[$row['whyshopseckill_id']]=$res[$key]['subtime'];


        }


    }
    asort($alllsub);
 $inactvie_id='';
    foreach ($alllsub as $key => $row) {
        $inactvie_id=$key;
        break;
      }
     // print_r($inactvie_id);exit;
    return $inactvie_id;
}

    /*获取当前时间的下一场*/
    function  getNextSeckill($start_time){
        $today_time=strtotime(date("Y-m-d"));
        $tomorrow_time=strtotime(date("Y-m-d")." 23:59:59");
        //只查询今天的
        $sql="select seckill_starttime from sdb_whyshopseckill_whyshopseckill where  seckill_starttime>".$start_time." and seckill_endtime<=".$tomorrow_time." order by seckill_starttime asc";
        $res=$this->db->selectRow($sql);
        return $res['seckill_starttime'];
    }

//获取秒杀商品图片和原价
function getSeckillGoodsInfo($goods_id){
  $sql_goods="select name,price,mktprice,image_default_id from sdb_b2c_goods where goods_id=".$goods_id;
  $row_goods=$this->db->selectRow($sql_goods);

  


  return $row_goods;
}

     //根据商品id查出是 否有这个商品id的秒杀活动

function getSeckillByGoodsId($goods_id,$whyshopseckill_id){
  $sql="select sdb_whyshopseckill_whyshopseckill_goods.* from sdb_whyshopseckill_whyshopseckill_goods  left join sdb_whyshopseckill_whyshopseckill on sdb_whyshopseckill_whyshopseckill_goods.whyshopseckill_id=sdb_whyshopseckill_whyshopseckill.whyshopseckill_id  where sdb_whyshopseckill_whyshopseckill_goods.seckill_goods_id=".$goods_id." and sdb_whyshopseckill_whyshopseckill.seckill_starttime<=".time()." and sdb_whyshopseckill_whyshopseckill.seckill_endtime>".time()."   order by whyshopseckill_id asc";
  $row=$this->db->selectRow($sql);
 
if($row) {
    $sql_whyshopseckill = "select * from sdb_whyshopseckill_whyshopseckill where whyshopseckill_id=" . ($whyshopseckill_id ? $whyshopseckill_id : $row['whyshopseckill_id']);
    //商品页直接过来的话，没有带get参数，所以还是得兼容一下
    $row_whyshopseckill = $this->db->selectRow($sql_whyshopseckill);

    $sql_goods = "select name,price from sdb_b2c_goods where goods_id=" . $goods_id;
    $row_goods = $this->db->selectRow($sql_goods);
    //创建数组



    $whyshopseckillinfo = $row_whyshopseckill;

    $whyshopseckillinfo['seckill_virtual_count']=$row['seckill_virtual_count'];
    $whyshopseckillinfo['seckill_total']=$row['seckill_total'];

    //商品id
    $seckillid = $row['seckill_goods_id'];
    $whyshopseckillinfo['goodsid'] = $seckillid;
    // 限购数量
    $limit = $row['seckill_limit'];
    $whyshopseckillinfo['limit'] = $limit;
    //秒杀价
    $seckillprice = $row['seckill_price'];
    $whyshopseckillinfo['seckillprice'] = $seckillprice;
    //原价
    $price = $row_goods['price'];
    $whyshopseckillinfo['price'] = $price;

    //距离秒杀结束时间

    // print_r($whyshopseckillinfo);exit();
    return $whyshopseckillinfo;
}
    else{
        return false;
    }
}



     //查询某会员在某秒杀时间段id下的某商品id的销售数量
function getSeckillgoodsnums($goods_id,$whyshopseckill_id){


  // $sql=select sum(i.nums) as total from sdb_b2c_order_items i
  // where i.goods_id=45
  // and i.whyshopseckill_id=55
  // $where_member="";


 if($whyshopseckill_id) {
     $sql = "select sum(i.nums) as total from sdb_b2c_order_items i
 where i.goods_id=" . $goods_id . "
 and i.whyshopseckill_id=" . $whyshopseckill_id . "
 and order_id in(select order_id from sdb_b2c_orders where pay_status='1')";

//print_r($sql);exit();
     $row = $this->db->selectRow($sql);


     $sellinfo = $row;

     return $sellinfo;
 }
    else{
        return false;
    }

}


function getSeckillgoodsnumslimit($goods_id,$whyshopseckill_id,$member_id){


  // $sql=select sum(i.nums) as total from sdb_b2c_order_items i
  // where i.goods_id=45 
  // and i.whyshopseckill_id=55 
  // $where_member="";
 // $sql="select sum(i.nums) as total from sdb_b2c_order_items i
 // where i.goods_id=".$goods_id." 
 // and i.whyshopseckill_id=".$whyshopseckill_id." and order_id in (select order_id from sdb_b2c_orders where member_id=".$member_id.")";



$sql="select sum(i.nums) as total from sdb_b2c_order_items i
 where i.goods_id=".$goods_id."
 and i.whyshopseckill_id=".$whyshopseckill_id."
and order_id in(select order_id from sdb_b2c_orders where pay_status='1' and member_id=".$member_id.")";

 $row=$this->db->selectRow($sql);

 //print_r($sql);exit();
 $member_purchase=$row;

 //print_r($member_purchase);exit;

 return $member_purchase;
}

}//End Class
